import {
  addToatlAchiev,
  delToatlAchiev,
  getMineResearch,
  getPersonScoreList,
  getTeamKycgRange,
  getTeamYdkyRange,
  getToDoList,
  getTotalAchiev,
  getTotalScoreList
} from "@/api/home/teamTotal";
import { addDialog } from "@/components/ReDialog";
import { dealDataCut } from "@/utils/common";
import { message } from "@/utils/message";
import ChooseAchiev from "@/views/research/achiev/form/chooseAchiev.vue";
import { h, onMounted, ref } from "vue";

import { getResearchList } from "@/api/other/researchTeam"; // 科研团队
import { getAchievList } from "@/api/research/achiev"; // 科研成果
import { getOtherAwardList } from "@/api/research/award"; // 科研获奖
import { getPaperList } from "@/api/research/paper"; // 论文
import { getPatentList } from "@/api/research/patent"; // 专利
import { getProjectList } from "@/api/research/project"; // 横向、纵向项目

import sumImg1 from "@/assets/home/sum1.png";
import sumImg2 from "@/assets/home/sum2.png";
import sumImg3 from "@/assets/home/sum3.png";
import sumImg4 from "@/assets/home/sum4.png";
import sumImg5 from "@/assets/home/sum5.png";
import sumImg6 from "@/assets/home/sum6.png";
import sumImg7 from "@/assets/home/sum7.png";

const selectOpt = ref({
  kycgList: [],
  tdList: [],
  hxList: [],
  zxList: [],
  lwList: [],
  zlList: [],
  hjList: []
});

export function useTotal() {
  const todoList = ref([
    { name: "论文", count: 0 },
    { name: "横向项目", count: 0 },
    { name: "纵向项目", count: 0 },
    { name: "专利", count: 0 },
    { name: "平台申报", count: 0 }
  ]);
  const wdkyList = ref([
    { name: "横向项目", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "纵向项目", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "论文", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "专利", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "平台申报", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "科研获奖", img: "", zs: 0 },
    { name: "教学成果", img: "", zs: 0 }
  ]);
  const zjfList = ref([]);
  const grndList = ref([]);
  const grydList = ref([]);
  const tdkycgList = ref([]);
  const tdydjfList = ref([]);
  const achievList = ref([]);

  // 选择科研成果
  const chooseAchievRef = ref();
  function addAchiev() {
    addDialog({
      title: "添加典型成果",
      sureBtnLoading: true,
      props: {
        formInline: {
          id: undefined,
          isIndex: 1,
          scrAchievementsIds: [],
          longitudinalProjectIds: [],
          horizontalProjectIds: [],
          otherObtainRewardIds: [],
          paperIds: [],
          patentIds: []
        },
        selectOpt: selectOpt.value || {}
      },
      width: "800px",
      draggable: true,
      fullscreenIcon: true,
      closeOnClickModal: false,
      contentRenderer: () =>
        h(ChooseAchiev, { ref: chooseAchievRef, formInline: null }),
      beforeSure: async done => {
        const curData = chooseAchievRef.value.getForm();
        function chores() {
          message("操作成功", {
            type: "success"
          });
          done(); // 关闭弹框
          laodAchievList(); // 刷新表格数据
        }

        await addToatlAchiev(curData);
        chores();
      }
    });
  }

  const delAchiev = async row => {
    await delToatlAchiev(row.id);
    message("删除成功", { type: "success" });
    laodAchievList();
  };

  const getUserList = item => {
    let allUserList = [];
    let data = item.researchTeam;
    if (data) {
      if (data.masterUser) allUserList.push(data.masterUser);
      if (data.secretaryUser) allUserList.push(data.secretaryUser);
      if (data.userList && data.userList.length > 0) {
        allUserList = allUserList.concat(data.userList);
      }
    }
    return allUserList;
  };

  const searchTotalData = () => {
    // 代表事项
    getToDoList().then(res => {
      let todoData = res.data;
      let tempTodoList = [
        { name: "论文", count: todoData.statusPaperCount || 0 },
        {
          name: "横向项目",
          count: todoData.statusHorizontalProjectsCount || 0
        },
        {
          name: "纵向项目",
          count: todoData.statusLongitudinalProjectsCount || 0
        },
        { name: "专利", count: todoData.statusPatentsCount || 0 },
        {
          name: "平台申报",
          count: todoData.statusPlatformDeclarationCount || 0
        }
      ];
      todoList.value = tempTodoList;
    });

    // 我的科研
    getMineResearch().then(res => {
      let data = res.data || {};
      let tempList = [
        {
          name: "横向项目",
          img: sumImg1,
          zs: data.horizontalProjectsCount || 0,
          jxz: data.statusHorizontalProjectsCount || 0,
          ywc:
            dealDataCut(
              data.horizontalProjectsCount,
              data.statusHorizontalProjectsCount
            ) || 0
        },
        {
          name: "纵向项目",
          img: sumImg2,
          zs: data.longitudinalProjectsCount || 0,
          jxz: data.statusLongitudinalProjectsCount || 0,
          ywc:
            dealDataCut(
              data.longitudinalProjectsCount,
              data.statusLongitudinalProjectsCount
            ) || 0
        },
        {
          name: "论文",
          img: sumImg3,
          zs: data.paperCount || 0,
          jxz: data.statusPaperCount || 0,
          ywc: dealDataCut(data.paperCount, data.statusPaperCount) || 0
        },
        {
          name: "专利",
          img: sumImg4,
          zs: data.patentsCount || 0,
          jxz: data.statusPatentsCount || 0,
          ywc: dealDataCut(data.patentsCount, data.statusPatentsCount) || 0
        },
        {
          name: "平台申报",
          img: sumImg5,
          zs: data.platformDeclarationCount || 0,
          jxz: data.statusPlatformDeclarationCount || 0,
          ywc:
            dealDataCut(
              data.platformDeclarationCount,
              data.statusPlatformDeclarationCount
            ) || 0
        },
        {
          name: "科研获奖",
          img: sumImg6,
          zs: data.otherObtainRewardCount || 0
        },
        {
          name: "教学成果",
          img: sumImg7,
          zs: data.teachingAchievementsCount || 0
        }
      ];
      wdkyList.value = tempList;
    });

    // 总积分
    getTotalScoreList({ pageNum: 1, pageSize: 9 }).then(res => {
      zjfList.value = res.data?.records || [];
    });

    // 个人年度
    getPersonScoreList({ type: 1, pageNum: 1, pageSize: 5 }).then(res => {
      grndList.value = res.data?.records || [];
    });

    // 个人月度
    getPersonScoreList({ type: 2, pageNum: 1, pageSize: 5 }).then(res => {
      grydList.value = res.data?.records || [];
    });

    // 团队科研
    getTeamKycgRange({ pageNum: 1, pageSize: 5 }).then(res => {
      tdkycgList.value = res.data?.records || [];
    });

    // 团队月度
    getTeamYdkyRange({ pageNum: 1, pageSize: 5 }).then(res => {
      tdydjfList.value = res.data?.records || [];
    });
  };

  // 获取项目集合
  const loadListOpt = async () => {
    let params = {
      // examineStatus: 1,
      orderBy: "desc",
      pageNum: 1,
      pageSize: 999
    };

    // 科研成果
    getAchievList(params).then(res => {
      selectOpt.value.kycgList = res?.data?.records || [];
    });

    // 团队
    getResearchList(params).then(res => {
      selectOpt.value.tdList = res?.data?.records || [];
    });

    // 项目类型 1纵向 2横向
    getProjectList({ projectType: 2, ...params }).then(res => {
      selectOpt.value.hxList = res?.data?.records || [];
    });

    getProjectList({ projectType: 1, ...params }).then(res => {
      selectOpt.value.zxList = res?.data?.records || [];
    });

    getPaperList(params).then(res => {
      selectOpt.value.lwList = res?.data?.records || [];
    });

    getPatentList(params).then(res => {
      selectOpt.value.zlList = res?.data?.records || [];
    });

    getOtherAwardList(params).then(res => {
      selectOpt.value.hjList = res?.data?.records || [];
    });
  };

  // 获取成果列表
  const laodAchievList = () => {
    getTotalAchiev({ pageNum: 1, pageSize: 200 }).then(res => {
      achievList.value = res.data?.records || [];
    });
  };

  onMounted(async () => {
    searchTotalData();

    // 获取成果列表
    laodAchievList();

    loadListOpt();
  });

  return {
    todoList,
    wdkyList,
    zjfList,
    grndList,
    grydList,
    tdkycgList,
    tdydjfList,
    achievList,
    addAchiev,
    delAchiev,
    getUserList
  };
}
